﻿Imports System.IO
Imports System.Text.RegularExpressions

Namespace Project

    ' Creates a simple list of projectfiles
    Public Class ProjectFileList

        Private _project As Project
        Private _projectfiles As New List(Of ProjectFile)
        Private _projectfolders As New List(Of ProjectFolder)

        ReadOnly Property Files As List(Of String)
            Get
                Dim filenames As New List(Of String)()
                For Each projectfile In _projectfiles
                    filenames.Add(projectfile.FullName)
                Next

                Return filenames
            End Get

        End Property

        ReadOnly Property RelativeFiles As List(Of String)
            Get
                Dim filenames As New List(Of String)
                For Each projectfile In _projectfiles
                    filenames.Add(projectfile.FullName.Replace(_project.DevelopmentFolder, ""))
                Next

                Return filenames
            End Get
        End Property

        ReadOnly Property ProjectFiles As List(Of ProjectFile)
            Get
                Return _projectfiles
            End Get
        End Property

        ReadOnly Property ProjectFolders As List(Of ProjectFolder)
            Get
                Return _projectfolders
            End Get
        End Property

        Public Function IsProjectFile(ByRef fullname As String) As Boolean
            For Each projectfile In _projectfiles
                If projectfile.FullName = fullname Then
                    Return True
                End If
            Next

            Return False
        End Function

        Public Function IsProjectFolder(ByRef fullname As String) As Boolean
            For Each profolder In _projectfolders
                If profolder.FullName = fullname Then
                    Return True
                End If
            Next

            Return False
        End Function

        Public Function GetProjectFile(ByRef fullname As String) As ProjectFile
            For Each projectfile In _projectfiles
                If projectfile.FullName = fullname Then
                    Return projectfile
                End If
            Next

            Throw New KeyNotFoundException("Could not find Project File")
            Return New ProjectFile("Not Found")
        End Function

        Public Function GetProjectFileByClassName(ByRef name As String) As ProjectFile
            For Each projectfile In _projectfiles
                If projectfile.UnrealClass.Name = name Then
                    Return projectfile
                End If
            Next

            Return Nothing
        End Function

        Public Function GetProjectFolder(ByRef fullname As String) As ProjectFolder
            For Each projectfolder In _projectfolders
                If projectfolder.FullName = fullname Then
                    Return projectfolder
                End If
            Next

            Throw New KeyNotFoundException("Could not find Project File")
            Return New ProjectFolder("Not Found")
        End Function

        Public Sub New(ByRef project As Project)
            _project = project
        End Sub

        Public Sub New(ByRef project As Project, ByRef rootdir As String, Optional ByRef regular_match As String = "", Optional ByVal recursive As Boolean = True)
            _project = project
            ScanDirectory(rootdir, regular_match, recursive)
        End Sub

        Public Sub RefreshDirectories()
            _projectfiles.Clear()
            _projectfolders.Clear()

            ScanDirectory(_project.DevelopmentFolder, Globals.kProjectFileRegex)

            _project.FileTreeView.FileTree.Nodes.Clear()
            _project.FileTreeView.ScanDirectory(_project.DevelopmentFolder, Globals.kProjectFileRegex)
            _project.FileTreeView.ExpandDefaultFolders()
        End Sub

        Public Sub ScanDirectory(ByRef folder As String, Optional ByRef regular_match As String = "", Optional ByVal recursive As Boolean = True)
            Dim re As New Regex(regular_match)

            Dim dir As New DirectoryInfo(folder)
            _projectfolders.Add(New ProjectFolder(dir))

            For Each fileinfo In dir.GetFiles()
                ' Check if filename matches regex
                If re.IsMatch(fileinfo.Name) Then
                    ' Add file to list
                    Dim actor_file As New ProjectFile(fileinfo.FullName)
                    _projectfiles.Add(actor_file)
                End If
            Next

            For Each dir In dir.GetDirectories()
                ScanDirectory(dir.FullName, regular_match, recursive)
            Next

        End Sub

    End Class

End Namespace
